//
//  WDCanlendarHandler.m
//  teatCalendar
//
//  Created by 解向前 on 2018/3/6.
//  Copyright © 2018年 com.test. All rights reserved.
//

#import "WDCanlendarHandler.h"
#import "WDCalendarCellModel.h"
#import "WDCalendarMonthModel.h"



@implementation WDCanlendarHandler


+ (instancetype)sharedInstance {
    static WDCanlendarHandler *instance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[self alloc] init];
    });
    return instance;
}

- (NSArray *)getAllMonthsForLastPage:(NSArray *)months {
    WDCalendarMonthModel *model = months[0];
    NSDate *date = model.pageMonth;
    NSDate *lastMonthDate = [self getLastMonthFirstDayDate:date];
    NSArray *lastMonthDates = [self getAllDateInCurrentCalendarPage:lastMonthDate];
    WDCalendarMonthModel *lastModel = [self getMonthModel:lastMonthDates pageMonth:lastMonthDate];
    NSMutableArray *arr = [NSMutableArray arrayWithObject:lastModel];
    for (int i = 0; i < months.count - 1; i++) {
        [arr addObject:months[i]];
    }
    return arr.copy;
}
- (NSArray *)getAllMonthsForNextPage:(NSArray *)months {
    WDCalendarMonthModel *model = months.lastObject;
    NSDate *date = model.pageMonth;
    NSDate *nextMonthDate = [self getNextMonthFirstDayDate:date];
    NSArray *nextMonthDates = [self getAllDateInCurrentCalendarPage:nextMonthDate];
    WDCalendarMonthModel *nextModel = [self getMonthModel:nextMonthDates pageMonth:nextMonthDate];
    NSMutableArray *marr = [NSMutableArray array];
    for (int i = 1; i < months.count; i++) {
        [marr addObject:months[i]];
    }
    [marr addObject:nextModel];
    return marr.copy;
}

- (NSArray *)getAllMonths:(NSDate *)date {
    NSDate *last = [self getLastMonthFirstDayDate:date];
    NSDate *next = [self getNextMonthFirstDayDate:date];
    NSArray *lastMonthDates = [self getAllDateInCurrentCalendarPage:last];
    NSArray *currentMonthDates = [self getAllDateInCurrentCalendarPage:date];
    NSArray *nextMonthDates = [self getAllDateInCurrentCalendarPage:next];
    WDCalendarMonthModel *lastModel = [self getMonthModel:lastMonthDates pageMonth:last];
    WDCalendarMonthModel *currentModel = [self getMonthModel:currentMonthDates pageMonth:date];
    WDCalendarMonthModel *nextModel = [self getMonthModel:nextMonthDates pageMonth:next];
    return @[lastModel, currentModel, nextModel];
}
- (NSArray *)getAllDateInCurrentCalendarPage:(NSDate *)date {
    NSMutableArray *days = [NSMutableArray array];
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *components = [calendar components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitWeekday fromDate:date];
    components.day = 1;
    NSDate *firstDay = [calendar dateFromComponents:components];
    components = [calendar components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitWeekday fromDate:firstDay];
    NSInteger weekday = components.weekday;
    if (weekday != 1) {
        NSDate *last = [self getLastMonthFirstDayDate:date];
        NSRange range = [calendar rangeOfUnit:NSCalendarUnitDay inUnit:NSCalendarUnitMonth forDate:last];
        components = [calendar components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitWeekday fromDate:last];
        for (int i = 0; i < (weekday - 1); i++) {
            components.day = range.length - (weekday - 2 - i);
            NSDate *day = [calendar dateFromComponents:components];
            [days addObject:day];
        }
    }
    NSRange range = [calendar rangeOfUnit:NSCalendarUnitDay inUnit:NSCalendarUnitMonth forDate:date];
    components = [calendar components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitWeekday fromDate:firstDay];
    NSInteger cday = 1;
    for (int i = 0; i < range.length; i++) {
        [components setDay:cday];
        NSDate *day = [calendar dateFromComponents:components];
        [days addObject:day];
        cday++;
    }
    NSLog(@"%ld", range.length);
    int nextCount = 7 - (range.length + weekday - 1) % 7;
    if (nextCount < 7) {
        NSDate *next = [self getNextMonthFirstDayDate:date];
        components = [calendar components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitWeekday fromDate:next];
        NSInteger cday = 1;
        for (int i = 0; i < nextCount; i++) {
            [components setDay:cday];
            NSDate *day = [calendar dateFromComponents:components];
            [days addObject:day];
            cday++;
        }
    }
    return days.copy;
}


- (NSDate *)getLastMonthFirstDayDate:(NSDate *)currentDate {
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *components = [calendar components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay fromDate:currentDate];
    NSInteger year = components.year;
    NSInteger month = components.month;
    NSInteger day = components.day;
    if (month == 1) {
        month = 12;
        year -= 1;
    } else {
        month -= 1;
    }
    day = 1;
    [components setDay:day];
    [components setMonth:month];
    [components setYear:year];
    NSDate *date = [calendar dateFromComponents:components];
    return date;
}

- (NSDate *)getNextMonthFirstDayDate:(NSDate *)currentDate {
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *components = [calendar components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay fromDate:currentDate];
    NSInteger year = components.year;
    NSInteger month = components.month;
    NSInteger day = components.day;
    if (month == 12) {
        month = 1;
        year += 1;
    } else {
        month += 1;
    }
    day = 1;
    [components setDay:day];
    [components setMonth:month];
    [components setYear:year];
    NSDate *date = [calendar dateFromComponents:components];
    return date;
}

- (NSArray *)datesToOndayCellModels:(NSArray *)dates {
    NSMutableArray *marr = [NSMutableArray array];
    [dates enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if ([obj isKindOfClass:[NSDate class]]) {
            WDCalendarCellModel *model = [[WDCalendarCellModel alloc] init];
            model.date = obj;
            [marr addObject:model];
        }
    }];
    return marr.copy;
}
- (WDCalendarMonthModel *)getMonthModel:(NSArray *)currentDates pageMonth:(NSDate *)pageMonth {
    NSMutableArray *days = [NSMutableArray array];
    for (int i = 0; i < currentDates.count; i++) {
        NSDate *date = currentDates[i];
        WDCalendarCellModel *dayModel = [[WDCalendarCellModel alloc] init];
        dayModel.date = date;
        [days addObject:dayModel];
    }
    WDCalendarMonthModel *monthModel = [[WDCalendarMonthModel alloc] init];
    monthModel.dayModels = days.copy;
    monthModel.pageMonth = pageMonth;
    return monthModel;
}

@end
